#!/usr/bin/env python

import sys
import pygame

from pygame.locals import *

pygame.init()
BACKGROUND_IMAGE = "tausta.jpg"
BALL_IMAGE = "pallo.png"
BALL_SIZE = 200



screen = pygame.display.set_mode((0,0), pygame.FULLSCREEN | pygame.HWSURFACE | pygame.DOUBLEBUF , 0)
SCREEN_WIDTH = screen.get_width()
SCREEN_HEIGHT = screen.get_height()

temp_background = pygame.image.load(BACKGROUND_IMAGE).convert()
ball = pygame.image.load(BALL_IMAGE).convert_alpha()
background = pygame.transform.smoothscale(temp_background, (SCREEN_WIDTH, SCREEN_WIDTH))

x,y = 0,0
movex,movey = 0,0

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

        elif event.type == KEYDOWN:
            if event.key == K_LEFT:
                movex=-6
            elif event.key == K_RIGHT:
                movex=6
            elif event.key == K_UP:
                movey=-6
            elif event.key == K_DOWN:
                movey=6
            elif event.key == K_ESCAPE:
                pygame.quit()
                sys.exit()
                
        elif event.type == KEYUP:
            if event.key == K_LEFT or  event.key == K_RIGHT:
                movex=0
            elif event.key == K_UP or  event.key == K_DOWN:
                movey=0
    
    x += movex
    y += movey
    
    if x >= SCREEN_WIDTH - BALL_SIZE:
        x = SCREEN_WIDTH -  BALL_SIZE
    elif x <= 0:
        x = 0
        
    if y >= SCREEN_HEIGHT - BALL_SIZE:
        y = SCREEN_HEIGHT -  BALL_SIZE
    elif y <= 0:
        y = 0

    screen.blit(background, (0,0))
    screen.blit(ball,(x,y))
    
    pygame.display.update()
